package com.april.pattern.creational.singleton;

import org.junit.platform.commons.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author April
 * @version V1.0.0
 * @date 2019/06/23 11:07
 * @description 容器单例
 * 多线程的情况下也不能保证单例唯一
 */
public class ContainerSingleton {
    private ContainerSingleton() {

    }

    private static Map<String, Object> singletonMap = new HashMap<>();

    public static void putInstance(String key, Object instance) {
        if (StringUtils.isNotBlank(key) && instance != null) {
            if (!singletonMap.containsKey(key)) {
                singletonMap.put(key, instance);
            }
        }
    }

    public static Object getInstance(String key) {
        return singletonMap.get(key);
    }
}
